package com.zfx.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zfx.pojo.Template;
import com.zfx.service.ITemplateService;
import com.zfx.util.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;

/**
 * @description:
 * @author: zfx
 * @version: 1.0
 * @time: 2020-05-09 13:52
 * <p>
 * 修改历史：
 * 修改日期      修改人员    版本    内容
 * 2020-05-09   zfx       1.0
 */
@RestController
@RequestMapping("/template")
public class TemplateController {

    @Autowired
    private ITemplateService templateService;

    @RequestMapping("/list")
    public JsonObject getList(@RequestBody String jsonData) throws IOException {
        JsonObject jsonObject = new JsonObject();
        ObjectMapper objectMapper = new ObjectMapper();
        HashMap<String, Object> dataMap = objectMapper.readValue(jsonData, HashMap.class);

        Template template = new Template();
        template.setModeId(dataMap.get("modeId") != null ? Integer.valueOf(String.valueOf(dataMap.get("modeId"))) : null);
        template.setModeName(dataMap.get("modeName") != null ? "%"+String.valueOf(dataMap.get("modeName"))+"%" : null);

        jsonObject.setCode("200");
        jsonObject.setMsg("success");
        jsonObject.setData("templateList", templateService.findByIdAndName(template, (int) dataMap.get("page"), (int) dataMap.get("limit") ));
        jsonObject.setDataSize(templateService.countByIdAndName(template));

        return jsonObject;
    }

    @RequestMapping("/save")
    public JsonObject save(@RequestBody Template template) throws IOException {
        JsonObject jsonObject = new JsonObject();

        int saveNum = templateService.save(template);

        if(saveNum == 1) {
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/findById")
    public JsonObject findById(@RequestParam(name = "modeId") int modeId) throws IOException {
        JsonObject jsonObject = new JsonObject();

        Template template = templateService.findById(modeId);

        if(template != null) {
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
            jsonObject.setData("template",template);
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/update")
    public JsonObject update(@RequestBody Template template) throws IOException {
        JsonObject jsonObject = new JsonObject();

        int updateNum = templateService.update(template);

        if(updateNum == 1) {
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/deleteById")
    public JsonObject deleteById(@RequestParam(name = "modeId") int modeId) throws IOException {
        JsonObject jsonObject = new JsonObject();

        System.out.println(modeId);

        int deleteNum = templateService.deleteById(modeId);

        if(deleteNum == 1) {
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/clientList")
    public JsonObject findAllByTemplate(@RequestBody String jsonData) throws IOException {
        JsonObject jsonObject = new JsonObject();
        ObjectMapper objectMapper = new ObjectMapper();
        HashMap<String, Integer> dataMap = objectMapper.readValue(jsonData, HashMap.class);

        Template template = new Template(dataMap);

        jsonObject.setCode("200");
        jsonObject.setMsg("success");
        jsonObject.setData("templateList",templateService.findByTemplate(template, dataMap.get("page"),dataMap.get("limit") ));
        jsonObject.setDataSize(templateService.countByTemplate(template));

        return jsonObject;
    }

}
